package com.dream.inspect.modules.app.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dream.inspect.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.modules.business.entity.InstructionsTargetEntity;
import com.dream.inspect.modules.business.service.InstructionsService;
import com.dream.inspect.modules.business.service.InstructionsTargetService;
import com.dream.inspect.modules.dream.pojo.InstrucationsTargetFormMap;
import com.dream.inspect.modules.dream.pojo.SysDeptFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;
import com.dream.inspect.modules.sys.service.SysDeptService;

@RestController
@RequestMapping("/app/instructions/")
public class AppInstructionsController extends AbstractController{

	@Autowired
    private InstructionsService instructionsService;
	@Autowired
	private InstructionsTargetService instructionsTargetService;
	@Autowired
	private SysDeptService sysDeptService;
	
	/**
	 * 指令列表
	 * @param params
	 * @return
	 */
	@RequestMapping("list")
	public R list(@RequestParam Map<String, Object> params){
		if(!params.containsKey("fbdwId")){
			/*SysUserEntity user = getUser();
			params.put("fbdwId", user.getDeptId());*/
			return R.error("请传入发布单位ID").put("data","请传入发布单位ID");
		}
		params.put("deleteFlag", 0);
		PageUtils page = instructionsService.findInstructionList(params);
		
		return R.ok().put("data", page.getList()).put("count",page.getTotalCount());
	}
	
	/**
	 * 获取指令下达部门
	 * @param params
	 * @return
	 */
	@RequestMapping("getInstructionsDept")
	public R getInstructionsDept(@RequestParam Map<String, Object> params){
		if(!params.containsKey("instructionsId")){
			return R.error("请输入指令ID").put("data","请输入指令ID");
		}
		List<InstrucationsTargetFormMap> datas = instructionsTargetService.findTargetByInstructionsId(params);
		return R.ok().put("data", datas);
	}
	
	/**
	 * 更新指令下达的部门的签收状态
	 * @param target
	 * @return
	 */
	@RequestMapping("target/update")
	@Transactional
	public R updateTargetEntity(InstructionsTargetEntity target){
		if(null == target.getId()){
			return R.error("请输入指令对象ID").put("data","请输入指令对象ID");
		}
		target.setJszt("1");
		target.setJssj(new Date());
		boolean result = instructionsTargetService.update(target,new EntityWrapper<InstructionsTargetEntity>().eq("id", target.getId()));
		if(result){
			return R.ok().put("data", "success");
		}else{
			return R.error("更新失败").put("data","更新失败");
		}
	}
	
	/**
	 * 新增指令
	 * @param params
	 * @return
	 */
	@RequestMapping("addEntity")
	public R addEntity(@RequestParam Map<String, Object> params){
		String dept = params.get("dept") + "";
		if(StringUtil.isEmpty(dept)){
			return R.error("请选择下达部门").put("data", "请选择下达部门");
		}
		if(!params.containsKey("fbdwId") || !params.containsKey("createUser")){
			return R.error("请输入发布单位ID和创建人ID").put("data", "请输入发布单位ID和创建人ID");
		}
		/*SysUserEntity user = getUser();
		params.put("fbdwId", user.getDeptId());
		params.put("createUser", user.getUserId());*/
		try {
			instructionsService.insertEntity(params);
		} catch (Exception e) {
			logger.error("add instructions error : " + e);
			return R.error("未知异常，请联系管理员").put("data", "未知异常，请联系管理员");
		}
		return R.ok().put("data", "success");
	}
	
	/**
	 * 获取所有下属部门
	 */
	@RequestMapping("getDepts")
	public R getDepts(@RequestParam Map<String, Object> params){
		if(!params.containsKey("dqdwId")){
			return R.error("请输入部门ID").put("data", "请输入部门ID");
		}
		List<SysDeptFormMap> list = sysDeptService.findTreeDept(params);
		return R.ok().put("data", list);
	}
	
	/**
	 * 获取下一级部门
	 * @param params
	 * @return
	 */
	@RequestMapping("getNextDepts")
	public R getNextDepts(@RequestParam Map<String, Object> params){
		if(!params.containsKey("dqdwId")){
			return R.error("请输入部门ID").put("data", "请输入部门ID");
		}
		List<SysDeptFormMap> list = sysDeptService.findNextDepts(params);
		return R.ok().put("data", list);
	}

	/**
	 * 获取当前部门非督察条线上级
	 * @param params
	 * @return
	 */
	@RequestMapping("getParentDept")
	public R getParentDept(@RequestParam Map<String, Object> params){
		if(!params.containsKey("dqdwId")){
			return R.error("请输入部门ID").put("data", "请输入部门ID");
		}
		List<SysDeptFormMap> list = sysDeptService.findDeptByParentCode(params);
		return R.ok().put("data", list);
	}


	/**
	 * 非督察条线获取下级
	 * @param params
	 * @return
	 */
	@RequestMapping("getNextNoDept")
	public R getNextNoDept(@RequestParam Map<String, Object> params){
		if(!params.containsKey("dqdwId")){
			return R.error("请输入部门ID").put("data", "请输入部门ID");
		}
		params.put("sfdctx","1");
		List<SysDeptFormMap> list = sysDeptService.findNextDeptByParentCode(params);
		return R.ok().put("data", list);
	}


	@RequestMapping("getAllOtherDept")
	public R getAllOtherDept(){
		Map<String, Object> params = new HashMap<String, Object>();
		SysUserEntity user = getUser();
		params.put("dqdwId",user.getDeptId());
		List<SysDeptFormMap> list = sysDeptService.findAllOtherDept(params);
		return R.ok().put("data", list);
	}


}
